//
//  ViewController.m
//  扫描二维码
//
//  Created by shenyanlong on 2017/8/31.
//  Copyright © 2017年 shenyanlong. All rights reserved.
//

#import "ViewController.h"

#import "QRViewController.h"

//屏幕的宽度
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
//屏幕的高度
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"二维码";
    
    UIButton *scanButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    scanButton.frame = CGRectMake((kScreenWidth - 200)/2, 100, 200, 30);
    [scanButton setTitle:@"扫描二维码" forState:(UIControlStateNormal)];
    [scanButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [scanButton addTarget:self action:@selector(scanAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:scanButton];
    
    UIButton *createButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    createButton.frame = CGRectMake((kScreenWidth - 200)/2, 150, 200, 30);
    [createButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [createButton setTitle:@"生成二维码" forState:(UIControlStateNormal)];
    [createButton addTarget:self action:@selector(createAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:createButton];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 100)/2, 200, 100, 100)];
    imageV.backgroundColor = [UIColor whiteColor];
    imageV.image = [UIImage imageNamed:@"头像.jpg"];
    [self.view addSubview:imageV];
}

#pragma mark -- 扫描二维码
- (void)scanAction
{
    NSLog(@"扫描二维码");
    QRViewController *vc = [[QRViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 生成(带头像的)二维码
- (void)createAction
{
    UIImageView *QRImgV = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 200)/2, 320, 200, 200)];
    QRImgV.backgroundColor = [UIColor whiteColor];
    //1.创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //2.恢复滤镜的默认属性
    [filter setDefaults];
    //3.给过滤器添加数据
    NSString *dataString = @"二维码";
    
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    //4.获取输出的二维码
    CIImage *outputImage = [filter outputImage];
    
    //5.显示高清二维码
    QRImgV.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
    
    //如果要带有头像的二维码,加上如下代码:
    UIImage *qrImage = QRImgV.image;
    
    //开启绘图,获取图形上下文  (上下文的大小,就是二维码的大小)
    UIGraphicsBeginImageContext(qrImage.size);
    //把二维码图片画上去. (这里是以,图形上下文,左上角为 (0,0)点)
    [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];

    //再把小图片画上去
    UIImage *smallImage = [UIImage imageNamed:@"头像.jpg"];
    CGFloat smallImageW = 40;
    CGFloat smallImageH = smallImageW;
    CGFloat smallImageX = (qrImage.size.width - smallImageW) * 0.5;
    CGFloat smallImageY = (qrImage.size.height - smallImageH) * 0.5;
    
    [smallImage drawInRect:CGRectMake(smallImageX, smallImageY, smallImageW, smallImageH)];
    //获取当前画得的这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    QRImgV.image = finalyImage;
    
    [self.view addSubview:QRImgV];

}

#pragma mark -- 获得高清二维码图片的方法
/**
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    CGColorSpaceRelease(cs);
    UIImage *returnImage = [UIImage imageWithCGImage:scaledImage];
    CGImageRelease(scaledImage);
    return returnImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
